<?php if (!defined('BASEPATH')) { die('No direct script access allowed'); }

class CI_Clickatellsms
{
	/**
	* The username for the Clickatell API
	* @access public
	* @var string
	*/
	public $username = null;
	
	/**
	* The password for the Clickatell API
	* @access public
	* @var string
	*/
	public $password = null;
	
	/**
	* Who will be shown as the sender of the text at the receivers handset.
	* @access public
	* @var string
	*/
	public $from = null;
	
	/**
	* The API id for this product.
	* @access public
	* @var string
	*/
	public $api_id = null;
	
	/**
	* The delay in minutes before the message is sent to the reciever.
	* @access public
	* @var integer
	*/
	public $delivery_delay = null;
	
	/**
	* Send the message under high priority
	* Could cost more to send the message!
	* @access public
	* @var integer
	*/
	public $escalate = 0;
		
	/**
	* All Available API Methods
	* @access public
	* @var array
	*/
	public $apiMethod = array(
		'send' => 'sendmsg',
		'balance' => 'getbalance',
		'canSend' => 'routeCoverage.php'
	);
	
	
	
##########################
#    Public Functions    #
##########################
	
	/**
	*
	*
	*
	*
	*/
	public function CI_clickatellsms($params=array())
	{
		$this->setup($params);
	}
	
	/**
	* Setup Class for SMS Sending
	* @param (array) $params: array of config options
	*/
	public function setup($params)
	{
		foreach($params as $key => $val)
		{
			$this->$key = $val;
		}
	}
	
	/**
	* @description: Send a message to the number provided
	* @param string $tel 
	*		The telephone number in international format.  Not inclduing a leading "+" or "00".
	* @param string $message 
	* 		The text message to send.
	* @return string
	*/
	public function send($tel, $message) 
	{
		
		$array = array(
			'api_id' => $this->api_id,
			'user' => $this->username,
			'password' => $this->password,
			'to' => $tel,
			'text' => $message
		);
			
		if($this->from != NULL)
		{
			$array['from'] = $this->from;
		}
		
		if($this->delivery_delay != NULL)
		{
			$array['deliv_time'] = $this->delivery_delay;
		}
		
		if($this->escalate != NULL)
		{
			$array['escalate'] = $this->escalate;
		}
		
		$postdata = http_build_query($array);
		
		$response = explode(':', $this->_sendApiRequest('send', $postdata));
		if($response[0] != 'ID')
		{
			return array('sent' => false, 'msg' => $response[1]);
		}
		else
		{
			return array('sent' => true, 'id' => $response[1]);
		}
	}
	
	/**
	* Get the balance, in credits, of your Clickatell account.
	* @return float
	*/
	public function getBalance() 
	{
		$postdata = http_build_query(
			array(
			'api_id' => $this->api_id,
			'user' => $this->username,
			'password' => $this->password
			)
		);
		
		$response = $this->_sendApiRequest('balance', $postdata);
		return $response;
	}
	
	/**
	* @description: Check if a phone number is covered and a message can be sent
	* @param string $number 
	* 		The telephone number to check for coverage
	* @return string
	*/
	public function isInCoverage($number) 
	{
		$postdata = http_build_query(
			array(
				'api_id' => $this->api_id,
				'user' => $this->username,
				'password' => $this->password,
				'msisdn' => $number
			)
		);
		
		$response = $this->_sendApiRequest('canSend', $postdata);
		return $response;
	}
	
	
#########################
# ! Private Functions ! #
#########################
	
	/**
	* @param (string) $api,
	* 		The API call you wish to make
	* @param (HTTPStream) $connection,
	* 		A _makeConnection() HTTPStream
	* @description: Retreives a API call response after submitting it
	* @return: string
	*/
	private function _sendApiRequest($api, $args)
	{
		$args = str_replace('&amp;', '&', $args);
		$url = 'http://api.clickatell.com/http/'.$this->apiMethod[$api].'?'.$args;
		return file_get_contents($url);
	}
}
?>